require 'rubygems'
require 'mechanize'
require 'iconv'
require 'nokogiri'
$LOAD_PATH << './'
require "crud.rb"


# encoding: utf-8

class GetEstateData
  include Crud

  #Constructor
  def initialize
    @array = nil;
    @agent = Mechanize.new{|a|
      a.user_agent_alias = 'Mac Safari';
    }
  end

  # Getting page with results
  #
  def get_result_page()
    url = "http://estate-base.net/estatebase/?menu=1&opr=2&tob=1&rgn=1&mas=5&obj=1&prb=&pre=&full=0&go=";
    url_forward = "http://estate-base.net/estatebase/?menu=1&forward=";
    result_page = @agent.get(url);
    num_pages_i = get_pages_amount(result_page);
    get_subpages(result_page); #get data from first page we visit

#    Searching pages amount and going through
    0.upto(num_pages_i) do
      forward_page = @agent.get(url_forward);
      page_number = get_page_number(forward_page)  #Just get page number at
      puts page_number.to_s + " page grabing..."
      get_subpages(forward_page);
    end

  end

  def get_subpages(page)
    prices = Array.new
    prices = get_prices(page)
    dates = get_dates(page)
      for i in (0..14)
        sub_page = @agent.get("http://estate-base.net/estatebase/notice.php?n=#{i}");
        sub_page.encoding = "windows-1251"
        get_parsed_info(sub_page, prices[i], dates[i])
      end
  end



  def get_parsed_info(page_data, price, date)
    info = page_data.search('//tr//td[@bgcolor = "#FFFF80"]').to_s;
    contact = page_data.search('//tr//td[@bgcolor = "#C0C0C0"]').to_s;
    if info.length > 1 && contact.length > 1
      p_info = info[22, info.length].gsub("</td>", "");
      p_contact = contact[24, info.length].gsub("</td>", "");
      if p_info.length > 0 && p_contact.length > 0
          Crud.insert(p_info.encode('UTF-8'), p_contact.encode('UTF-8'), price, date, 'onerooms');
#        puts p_info.encode('UTF-8').encoding
      else
          puts "Wrong data, no insertion"
      end
    end
  end

  def get_prices(page)
    full = page.search('.//body//table//*//td[@class = "mn"]//table//*//tr//td//table').to_s
    price_regexp =/\d...>\d+</
    arra = full.scan(price_regexp)
    prices = Array.new
    for i in arra do
      regexp = />\d+/
      str = i.to_s.match(regexp)
      prices.push(str.to_s[1, str.length+3])
    end
      return prices
  end

  def get_dates(page)
    full = page.search('.//body//table//*//td[@class = "mn"]//table//*//tr//td//table').to_s
    dates_regexp = /\d+\D\d+\D\d\d\d\d/
    dates_p = full.scan(dates_regexp)
    dates = Array.new
    for i in dates_p do
     a = dates.push(i.gsub(/\W/, '-'))
    end
    return dates
  end

  #
  # @Return how much pages on search result
  # @Type Integer
  #
  def get_pages_amount(page)
    pages_s= (page.body.match('\D\.\d{1,2}\D\D\D\D\d{1,2}').to_s);
    return pages_s[pages_s.length-2, pages_s.length].to_i;
  end

  #
  # @Return page number we currently at
  # @Type Integer
  #
  def get_page_number(page)
    pages_s= (page.body.match('\D\.\d{1,2}\D\D\D\D\d{1,2}').to_s);
    return pages_s[2,2].to_i;
  end
end


gesd = GetEstateData.new;
gesd.get_result_page;
